#pragma once

class CGameObject;
class CCamera;

#define MAX_ATTRIBUTES          500
#define MAX_TEXTURES            100
#define MAX_MATERIALS           100
#define MAX_LIGHTS              50 

#include <vector>

class CScene
{
public:
	CScene(void);
	~CScene(void);

private:
    LPDIRECT3DDEVICE9			m_pD3DDevice;                   

	//int						m_nObjects;
	//CGameObject				*m_pObjects;
	std::vector<CGameObject*>	m_lObjects;
	CGameObject*				m_pEffect[4];
	D3DXMATRIX					EffectTranslation[4];
	bool						m_bEffect;
	D3DXMATRIX					EffectTextureTranslation;
	
	CCamera						*m_pCamera;    
    //CPlayer						*m_pPlayer; 
	std::vector<CPlayer*>		m_plPlayer;
	int							m_ControlPlayer;

	

	CTerrain					*m_pTerrain;
	CSkyBox						*m_pSkyBox;
	

    int							m_nCurrentMaterial;               
    int							m_nCurrentTexture;                
public:
    D3DMATERIAL9				*m_ppd3dMaterials[MAX_MATERIALS]; 
    TEXTUREITEMSTRUCT			*m_ppTextureItems[MAX_TEXTURES];  
    int							m_nMaterials;               
    int							m_nTextures;                

	int GetCurrentMaterial() { return(m_nCurrentMaterial); }
	void SetCurrentMaterial(int nMaterial) { m_nCurrentMaterial = nMaterial; }
	int GetCurrentTexture() { return(m_nCurrentTexture); }
	void SetCurrentTexture(int nTexture) { m_nCurrentTexture = nTexture; }

	int AddMaterial(D3DMATERIAL9 *pd3dMaterial);
	void AddTextureItem(TEXTUREITEMSTRUCT *pTextureItem) { m_ppTextureItems[m_nTextures++] = pTextureItem; }

    D3DLIGHT9					*m_ppd3dLights[MAX_LIGHTS];    
	bool						m_pbLightsEnabled[MAX_LIGHTS];
    int							m_nLights;                  

    D3DMATERIAL9				m_d3dDefaultMaterial;  
	LPDIRECT3DTEXTURE9			m_pd3dDefaultTexture;

	int							m_nRotatingObject;
	int							m_nMovingObject;

	void SetD3DDevice(LPDIRECT3DDEVICE9 pD3DDevice) { m_pD3DDevice = pD3DDevice; if (m_pD3DDevice) m_pD3DDevice->AddRef(); }
	void SetCamera(CCamera *pCamera) { m_pCamera = pCamera; }


	void OnChangeCamera(ULONG nCameraMode, float fTimeDistance);

	CPlayer* GetPlayer(void) { return m_plPlayer[m_ControlPlayer]; }
	CPlayer* GetPlayer(int Index) { return m_plPlayer[Index]; }
	CCamera* GetCamera(void) { return m_pCamera; }
	CCamera* GetCamera(int Index) { return  m_plPlayer[Index]->GetCamera(); }


	static ATTRIBUTEITEMSTRUCT CollectAttributeData(LPVOID pContext, LPWSTR pstrTextureFileName, const D3DMATERIAL9 *pd3dMaterial);
	static int CollectTexture(LPVOID pContext, LPWSTR pstrTextureFileName, int nMipMapLevels=0);

	int PickObjectPointedByCursor(int xClient, int yClient);

	void BuildObjects();
	void ReleaseObjects();
    void AnimateObjects(float fElapsedTime);
	void MoveObjects(int nMovingObject, float x, float y, float z);
	void RotateObjects(int nRotatingObject, float fPitch, float fYaw, float fRoll);

	void RenderObjects();

	void Render();

	void OnEffect(void);
	void OffEffect(void);
};